from datetime import datetime

from django.db import models
from django.utils.safestring import mark_safe 
from project.models import ProjectList
from django.db.models import Q

class Company(models.Model):
    '''
    客户公司信息管理
    '''
    type_choices=(
        ('0','建设单位'),
        ('1','设计单位'),
        ('2','施工单位'),
        ('3','监理单位'),
        ('4','勘察单位'),
        ('5','设计单位'),
        ('6','协作单位'),
        ('7','审图单位'),
        ('8','咨询单位'),
        ('9','技术外包'),
        ('10','劳务外包'))
    shortname = models.CharField(max_length=50, blank=True,verbose_name='公司简称')
    fullname = models.CharField(max_length=200,verbose_name='公司全称')
    companytype = models.CharField(choices=type_choices, default='0',max_length=50, verbose_name='公司类型')#事业单位、国企、私人企业、上市公司、合伙人（挂靠）
    uscc = models.CharField(max_length=100,blank=True,verbose_name='统一社会信用代码')
    ceo = models.CharField(max_length=10,blank=True,verbose_name='法定代表人')
    ceoid = models.CharField(max_length=50,blank=True,verbose_name='法人身份证')
    bankname = models.CharField(max_length=100,blank=True,verbose_name='开户行')
    accountnumber = models.CharField(max_length=100,blank=True,verbose_name='账号信息')
    address = models.CharField(max_length=200,blank=True,verbose_name='通讯地址')
    enable = models.BooleanField(default=True,verbose_name='可用状态')
    desc = models.CharField(max_length=300, verbose_name='备注信息',blank=True)
    addtime = models.DateTimeField('添加时间',default=datetime.now)
    class Meta:
        verbose_name = '客户公司信息'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.fullname

class Contacter(models.Model):
    '''
    客户联系人信息
    '''
    company = models.ForeignKey(Company,verbose_name='所属公司',related_name='rigs',on_delete=models.DO_NOTHING)
    name = models.CharField(max_length=20,blank=True,verbose_name='联系人姓名')
    tel1 = models.CharField(max_length=20,blank=True,verbose_name='电话1')
    tel2 = models.CharField(max_length=20,blank=True,verbose_name='电话2')
    tel3 = models.CharField(max_length=20,blank=True,verbose_name='电话3')
    position = models.CharField(max_length=20,blank=True,verbose_name='职位')
    enable = models.BooleanField(default=True,verbose_name='可用状态')
    desc = models.CharField(max_length=300, verbose_name='备注信息',blank=True)
    addtime = models.DateTimeField('添加时间',default=datetime.now)
    add_by = models.ForeignKey('users.UserProfile',default=1,null=False,blank=True,verbose_name='添加人',on_delete=models.DO_NOTHING)
    class Meta:
        verbose_name = '客户联系人'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

class RigData(models.Model):
    '''
    钻机联系信息
    '''
    praise_stauts = (
        ('5','技术好价格优'),
        ('4','技术好价格高'),
        ('3','技术价格一般'),
        ('2','技术差价格高'),
        ('1','技术差价格低'))
    name = models.CharField(max_length=20,verbose_name='钻机联系人')
    tel1 = models.CharField(max_length=20,blank=True,verbose_name='电话1')
    tel2 = models.CharField(max_length=20,blank=True,verbose_name='电话2')
    tel3 = models.CharField(max_length=20,blank=True,verbose_name='电话3')
    city = models.CharField(max_length=20,verbose_name='所在城市')
    praise = models.CharField(choices=praise_stauts,default='3',max_length=50,verbose_name='技能评价')
    isactive = models.BooleanField(default=True,verbose_name='是否在用')
    desc = models.TextField(verbose_name='已合作情况',blank=True)
    addtime = models.DateTimeField(auto_now_add=True,verbose_name='添加时间')
    add_by = models.ForeignKey('users.UserProfile',verbose_name='添加人',blank=True,on_delete=models.DO_NOTHING)

    class Meta:
        verbose_name = '钻机数据库'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
    
    def same_city_boss(self):
        html = '<a href="?_p_city__in='+ self.city +'">'+ self.city +'</a>'
        return mark_safe(html)
    same_city_boss.short_description = '同城钻机'

    def get_worknum(self):#增加完成工作量列
        myworks = RigWork.objects.filter(rigboss=self.pk)
        myproject = myworks.count()
        myholes = 0
        mydepths = 0
        html = ''
        for mywork in myworks:
            html += ''
            myholes += mywork.holes
            mydepths += mywork.depths
            
        # self.RigWork_set.all().count()
        # Course.objects.filter(course_org=course_org).count()
        # html = '<a data-res-uri="/project/map_show/" data-edit-uri="/xadmin/users/userwork/9/update/" class="details-handler" rel="tooltip" title="南玉贤详情"><i class="fa fa-info-circle"></i></a>'
        html="<a href='rigwork/?_rel_rigboss__id__exact=1'>完成%s个项目</a>,共计%s孔,进尺%sm" % (myproject,myholes,mydepths)
        return  mark_safe(html)
    get_worknum.short_description = '已完成情况'

class RigWork(models.Model):
    '''
    钻机工作量
    '''
    rigboss = models.ForeignKey(RigData,verbose_name='钻机老板',related_name='works',on_delete=models.DO_NOTHING)
    project = models.ForeignKey(ProjectList,verbose_name='工地名称',related_name='rigworks',on_delete=models.DO_NOTHING)
    holes = models.IntegerField(default=0,verbose_name='完成钻孔数（个）')
    depths = models.IntegerField(default=0,verbose_name='完成进尺（m）')
    price = models.IntegerField(default=0,verbose_name='综合单价（元/m）')
    total = models.IntegerField(default=0,verbose_name='结算总额（元）')
    starttime = models.DateTimeField(blank=True,verbose_name='外业开始时间')
    endtime = models.DateTimeField(blank=True,verbose_name='外业结束时间')
    addtime = models.DateTimeField(auto_now_add=True,verbose_name='添加时间')
    add_by = models.ForeignKey('users.UserProfile',verbose_name='添加人',null=False,blank=True,on_delete=models.DO_NOTHING)
    class Meta:
        verbose_name = '填报钻机工作量'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.rigboss.name